package com.itheima.webaitliasserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.webaitliasserver.mapper.ClazzMapper;
import com.itheima.webaitliasserver.pojo.Clazz;
import com.itheima.webaitliasserver.pojo.PageResult;
import com.itheima.webaitliasserver.request.ClassInfo;
import com.itheima.webaitliasserver.request.QueryClassListRequest;
import com.itheima.webaitliasserver.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: xiaoZhang
 * @Description:
 * @Author: Zhang
 * @CreateDate: 2025-09-08 9:10
 */
@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult<Clazz> queryClazzList(QueryClassListRequest request) {
        if(request == null){
            return new PageResult<>(0L,null);
        }
        PageHelper.startPage(request.getPage() , request.getPageSize()); // 设置分页数量

        List<Clazz> clazzList = clazzMapper.classListMapper(request);

        clazzList.stream().forEach(clazz -> {
            LocalDate currentDate = LocalDate.now();
            if (currentDate.isAfter(clazz.getEndDate())) {
                clazz.setStatus("已结课");
            } else if (currentDate.isBefore(clazz.getBeginDate())) {
                clazz.setStatus("未开班");
            } else {
                clazz.setStatus("在读中");
            }
        });

        log.info("clazzList:{}" , clazzList.toString());
        PageInfo<Clazz> pageInfo  = new PageInfo<>(clazzList);
        return new PageResult<>(pageInfo.getTotal() , pageInfo.getList());
    }

    @Override
    public void removeClassById(Integer id) {
        clazzMapper.removeClassByIdMapper(id);
    }

    /**
     * 添加班级信息
     * @param classRequest
     */
    @Override
    public void addClassInfo(ClassInfo classRequest) {
        classRequest.setCreateTime(LocalDateTime.now());
        classRequest.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClassInfoMapper(classRequest);
    }
}
